<?php

namespace app\web\controller;
use app\web\model\LineAttributesModel;
use app\web\model\AgentModel;
use app\adminmall\model\PaylogModel;
use app\common\controller\StoreBaseController;
use think\Db;

/**
 * 短信充值
 */
class SmsRechargeController extends StoreBaseController
{
	protected $model;
	protected $paylogmodel;
		
    protected function _initialize() {
        parent::_initialize();
		$this->model = new AgentModel();
		$this->paylogmodel = new PaylogModel();
    }
	
    /**
     * 充值短信
     * @author [longx] 
     */
    public function index(){
	    $storeinfo = session('storeInfo');		
		$list = $this->model->where('id',session('storeInfo')['id'])->find();		
		$this->assign('smscount',$list['smscount']);
		$this->assign('store_balance',$list['store_balance']);
        return $this->fetch();
    }
	
	public function smsgm(){
		$storeinfo = session('storeInfo');	
    	$param = input('post.');	
		$id = input('smsid');
		$price = input('smsprice');
		$count = input('smscount');		
		$list = $this->model->where('id',$storeinfo['id'])->find();	
		if($price>$list['store_balance']){
			return json(['code'=>408,'msg'=>"店铺余额不足，请充值！"]);
		}		
		if($id){

			
			$param['uid'] = $storeinfo['u_id'];
			$param['user_type'] = 2;
			$param['price'] = $price;
			$param['type'] = 'sms';
			$param['sn'] = order_create_no();
			$param['add_time'] = now_time();
			$param['tid'] = $storeinfo['id'];
			$param['pay_type'] = 1;
			$param['status'] = 2;
			
			if ($this->paylogmodel->allowField(true)->save($param) !== false) {
				Db::name('mall_agent')->where('id', $storeinfo['id'])->setInc('smscount', $count);
				Db::name('mall_agent')->where('id', $storeinfo['id'])->setDec('store_balance', $price);
				$store = $this->model->where('id',$storeinfo['id'])->find($storeinfo['id']);	
				$data['smscount'] = $store['smscount'];
				$data['store_balance'] = $store['store_balance'];
				return json(['code'=>200,'data'=>$data,'msg'=>"购买成功"]);
			}else{
				return json(['code'=>408,'msg'=>"购买失败"]);
			}
		}    	
	}
}